<!DOCTYPE html>
<html>
<head>
  <title>弹窗示例</title>
  <style>
    .popup {
        position: relative;
        left: 50%;
        transform: translate(-50%);
        width: 640px;
        margin-top: 65px;
        background-color: #fff;
        background-clip: padding-box;
        border: 0;
        border-radius: 4px;
        box-shadow: 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 9px 28px 8px rgba(0, 0, 0, 0.05);
        pointer-events: auto;
    }
    .popup-title {
        padding: 16px 24px;
        color: rgba(0, 0, 0, 0.65);
        background: #fff;
        border-bottom: 1px solid #f0f0f0;
        border-radius: 4px 4px 0 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;
    }
    .popup-title h3 {
        margin: 0;
        color: rgba(0, 0, 0, 0.85);
        font-weight: 500;
        font-size: 16px;
        line-height: 22px;
        word-wrap: break-word;
    }
    .popup-close {
        cursor: pointer;
        color: rgba(0, 0, 0, 0.45);
        font-weight: 700;
    }
    .popup-line{
        height: 1px;
        width: 500px;
        color: #f0f0f0;
    }
    .popup-content {
        padding: 24px;
        font-size: 14px;
        line-height: 1.5715;
        word-wrap: break-word;
        /* border-bottom: 1px solid #f0f0f0; */
    }
    .popup-item{
        display: flex;
        flex-direction: column;
        height: 54px;
        /* height: 40px; */
        /* align-items: center; */
        /* margin-bottom: 25px; */
    }
    .popup-label span {
        margin-right: 5px;
        color: red;
    }
    .popup-input-wrapper {
        display: flex;
    }
    .popup-label-text {
        display: flex;
        justify-content: end;
        align-items: center;
        height: 32px;
        color: rgba(0, 0, 0, 0.85);
        border-radius: 4px;
        font-size: 14px;
        width: 80px;
        white-space: nowrap;
        margin-right: 10px;
    }
    .popup-input {
        width: 500px;
        box-sizing: border-box;
        margin: 0;
        padding: 0;
        font-variant: tabular-nums;
        list-style: none;
        font-feature-settings: 'tnum', "tnum";
        display: inline-block;
        min-width: 0;
        padding: 4px 11px;
        font-size: 14px;
        color: rgba(0, 0, 0, 0.65);
        line-height: 1.5715;
        background-color: #fff;
        background-image: none;
        border: 1px solid #d9d9d9;
        border-radius: 4px;
        transition: all 0.3s;
        outline-color: #ff8a5c;
    }
    .popup-input::placeholder{
        color: #bfbfbf;
    }
    .popup-select{
        width: 500px;
        box-sizing: border-box;
        margin: 0;
        padding: 0;
        font-variant: tabular-nums;
        list-style: none;
        font-feature-settings: 'tnum', "tnum";
        display: inline-block;
        min-width: 0;
        padding: 4px 11px;
        font-size: 14px;
        color: rgba(0, 0, 0, 0.65);
        line-height: 1.5715;
        background-color: #fff;
        background-image: none;
        border: 1px solid #d9d9d9;
        border-radius: 4px;
        transition: all 0.3s;
        outline-color: #ff8a5c;
    }
    .popup-select option {
        background-color:#fff;
        color: rgba(0, 0, 0, 0.65);
    }
    .default-option {
        color: #bfbfbf; 
    }
    .popup-select option:checked {
        background-color: #fff6f0;
        font-weight: 600;
    }
    /* .popup-select option:hover{
        background-color: #f5f5f5;
    } */
    /* .select-wrapper .dropdown-menu option:hover {
      background-color: #f5f5f5;
    } */
/*     
    .popup-select  {
        position: relative;
    } */

  /* .popup-select::after { */
        /* content: "";
        position: absolute;
        top: 50%;
        right: 10px;
        transform: translateY(-50%);
        width: 0;
        height: 0;
        border-left: 6px solid transparent;
        border-right: 6px solid transparent;
        border-top: 6px solid red; 修改箭头的颜色 */
        /* color:rgba(0, 0, 0, 0.65)
  } */

  /* 隐藏原生的下拉箭头 */
  /* select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    padding-right: 20px;
    background: none;
  } */

    .popup-textarea {
        box-sizing: border-box;
        border-radius: 4px;
        padding: 9px 11px 4px;
        width: 500px;
        height: 125px;
        font-size: 14px;
        color: rgba(0, 0, 0, 0.65);
        line-height: 1.5715;
        list-style: none;
        min-height: 32px;
        line-height: 1.5715;
        vertical-align: bottom;
        transition: all 0.3s, height 0s;
        border: 1px solid #d9d9d9;
        outline-color: #ff8a5c;
    }
    .popup-info{
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    .popup-textarea::placeholder{
        color: #bfbfbf;
    }
    .popup-footer {
        display: flex;
        justify-content: flex-end;
        margin-top: 20px;
        padding: 10px 16px;
        text-align: right;
        background: transparent;
        border-top: 1px solid #f0f0f0;
        border-radius: 0 0 4px 4px;
    }
    .popup-button {
        margin-left: 10px;
        padding: 5px 10px;
        cursor: pointer;
        line-height: 1.5715;
        position: relative;
        display: inline-block;
        font-weight: 400;
        white-space: nowrap;
        text-align: center;
        background-image: none;
        border: 1px solid transparent;
        box-shadow: 0 2px 0 rgba(0, 0, 0, 0.015);
        cursor: pointer;
        transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        touch-action: manipulation;
        height: 32px;
        padding: 4px 15px;
        font-size: 14px;
        border-radius: 4px;
        border-color: #d9d9d9;
    }
    .popup-button-cancel{
        color: rgba(0, 0, 0, 0.65);
        background: #fff;
    }
    .popup-button-confirm{
        color: #fff;
        background: #f03232;
    }
    .popup-error {
        font-size: 14px;
        color: #f5222d;
        margin-left: 90px;
    }
    .popup-input-error {
        border: 1px solid #f5222d;
    }
    /* #popup-title-input.active {
        border: 1px solid #f5222d;
    } */
    #charCount{
        position: relative;
        right: auto;
    }
    #charCount span{
        color: #f5222d;
    }
    .input-error {
        border: 1px solid red;
    }
  </style>
</head>
<body>
  <button onclick="showPopup()">反馈</button>
  <div id="popup" class="popup">
    <div class="popup-title">
      <h3>意见反馈</h3>
      <span class="popup-close" onclick="hidePopup()">X</span>
    </div>
    <div class="popup-content">
        <div class="popup-item">
            <div class="popup-label">
                <div class="popup-input-wrapper">
                <div class="popup-label-text"><span>*</span>标题:</div>
                <input id="popup-title-input" class="popup-input" type="text" placeholder="请输入标题" required>
                </div>
            </div>
            <div id="popup-title-error" class="popup-error"></div>
        </div>
        <div class="popup-item">
            <div class="popup-label">
                <div class="popup-input-wrapper">
                <div class="popup-label-text"><span>*</span>意见反馈:</div>

                <select id="popup-feedback-select" class="popup-select default-option" onclick="selectValue()" required>
                    <option selected hidden disabled value="">请选择意见类型</option>
                    <option value="option1">选项1</option>
                    <option value="option2">选项2</option>
                    <option value="option3">选项3</option>
                    <option value="option3">选项4</option>
                    <option value="option3">选项5</option>
                    <option value="option3">选项6</option>
                    <option value="option3">选项7</option>
                    <option value="option3">选项8</option>
                    <option value="option3">选项9</option>
                    <option value="option3">选项10</option>
                    <option value="option3">选项11</option>
                </select>


                </div>
            </div>
            <div id="popup-feedback-error" class="popup-error"></div>
        </div>
        <div class="popup-item" style="height: 147px;">
            <div class="popup-label">
                <div class="popup-input-wrapper">
                <div class="popup-label-text"><span>*</span>详细内容:</div>
                <textarea id="popup-details-textarea" class="popup-textarea" placeholder="请输入详细内容,不超过200字" required></textarea>
                </div>
            </div>
            <div class="popup-info">
                <div id="popup-details-error" class="popup-error"></div>
                <div id="charCount">(<span id='enter-number'>0</span>/200)</div>
            </div>
        </div>
    </div>
    <div class="popup-footer">
        <button class="popup-button popup-button-cancel" onclick="resetForm()">取消</button>
        <button class="popup-button popup-button-confirm" onclick="validateForm()">确定</button>
    </div>
    </div>
  </div>

  <script>
    //发请求的函数
    function sendRequest(method, url, data, headers, callback) {
  var xhr = new XMLHttpRequest();

  xhr.open(method, url, true);

  // 设置请求头
  if (headers) {
    for (var header in headers) {
      xhr.setRequestHeader(header, headers[header]);
    }
  }

  xhr.onreadystatechange = function() {
    if (xhr.readyState === XMLHttpRequest.DONE) {
      if (xhr.status >= 200 && xhr.status < 400) {
        var response = JSON.parse(xhr.responseText);
        callback(null, response);
      } else {
        callback(new Error('Request failed with status ' + xhr.status));
      }
    }
  };

  xhr.onerror = function() {
    callback(new Error('Request failed'));
  };

  // 发送请求
  if (data) {
    xhr.send(JSON.stringify(data));
  } else {
    xhr.send();
  }
}

    // 获取元素
    var titleInput = document.getElementById("popup-title-input");
    var feedbackSelect = document.getElementById("popup-feedback-select");
    var detailsTextarea = document.getElementById("popup-details-textarea");
    const charCount = document.getElementById('charCount');
    const enterNumber = document.getElementById('enter-number');

    // 添加监听
    titleInput.addEventListener("input", validateTitle);
    feedbackSelect.addEventListener("input", validateFeedback);
    detailsTextarea.addEventListener("input", validateDetails);
    detailsTextarea.addEventListener('input', showDetailsNumber);

    //选中项后更改颜色
    function selectValue(){
        feedbackSelect.addEventListener('change', function(){
            feedbackSelect.classList.remove('default-option');
        })
    }

    // 输入详细内容时将下面的字数与输入内容保持一致
    function showDetailsNumber(){
        const text = detailsTextarea.value.trim();
        enterNumber.textContent = `${text.length}`;
    }

    // 标题的校验
    function validateTitle() {
        var isValid = true;
        if (titleInput.value.trim() === "") {
            showErrorMessage("popup-title-error", "标题不能为空");
            titleInput.classList.add('input-error');
            isValid = false;
        } else {
            clearErrorMessage("popup-title-error");
            titleInput.classList.remove('input-error');
        }

        return isValid;
    }
    // 意见反馈的校验
    function validateFeedback() {
        var isValid = true;
        if (feedbackSelect.value.trim() === "") {
            showErrorMessage("popup-feedback-error", "意见类型不能为空");
            feedbackSelect.classList.add('input-error');
            isValid = false;
        } else {
            clearErrorMessage("popup-feedback-error");
            feedbackSelect.classList.remove('input-error');
        }
        return isValid;
    }

    // 详细内容的检验
    function validateDetails() {
        var isValid = true;
        if (detailsTextarea.value.trim() === "") {
            showErrorMessage("popup-details-error", "请输入详细内容");
            detailsTextarea.classList.add('input-error');
            isValid = false;
        } else if (detailsTextarea.value.trim().length > 200) {
            showErrorMessage("popup-details-error", "详细内容不能超过200个字符");
            detailsTextarea.classList.add('input-error');
            isValid = false;
        } else {
            clearErrorMessage("popup-details-error");
            detailsTextarea.classList.remove('input-error');
        }
        return isValid;
    }

    // 提交前整体的校验
    function validateForm() {
        clearErrorMessage();
        var isTitleValid = validateTitle();
        var isFeedbackValid = validateFeedback();
        var isDetailsValid = validateDetails();
        if (isTitleValid && isFeedbackValid && isDetailsValid) {
            console.log(isTitleValid)
            const data = {
                title: titleInput.value,
                feedbackType: feedbackSelect.value ,
                feedbackContent:detailsTextarea.value
            }
            //  ajaxService.addFeedback(vals).then(() => {
			// 		message.success('发送成功');
			// 	});
            // let data ={"data":{"title":"烦烦烦","feedbackType":"邮件退信","feedbackContent":"反反复复"},"appVersion":"WEB","device":{"deviceId":"H5","deviceMake":"H5","deviceModel":"H5","sysType":"WEB","sysVersion":"H5"},"time":1687942382152,"sign":"","token":"DAB11D99668A84401594FA4B696AA96D","lang":"zh","systemId":"10000"}
            sendRequest('POST', 'http://apigw.tradechina.com/api-supplier/feedBack/addFeedback.json', {...data}, null, function(error, response) {
                if (error) {
                    console.error('Request failed:', error);
                } else {
                    console.log('Response:', response);
                }
            });
            resetForm();
            hidePopup();
        }
    }

    //显示错误信息
    function showErrorMessage(errorId, message) {
        var errorElement = document.getElementById(errorId);
        errorElement.innerHTML = message;
    }

    // 清楚错误提示
    function clearErrorMessage(errorId) {
    if (errorId) {
        var errorElement = document.getElementById(errorId);
        errorElement.innerHTML = "";
    } else {
        var errorElements = document.querySelectorAll(".popup-error");
            errorElements.forEach(function(element) {
            element.innerHTML = "";
        });
    }
    }

    // 重置表单
    function resetForm() {
        titleInput.value = "";
        feedbackSelect.value = "";
        detailsTextarea.value = "";
        const text = detailsTextarea.value;
        // charCount.textContent = `(${text.length}/200)`;
        enterNumber.textContent = `${text.length}`;
        enterNumber.style.color = 'red';
        clearErrorMessage();
    }

    // 展示弹窗
    function showPopup() {
      resetForm();
      document.getElementById("popup").style.display = "block";
    }

    // 隐藏弹窗
    function hidePopup() {
      document.getElementById("popup").style.display = "none";
    }

  </script>
</body>
</html>

